home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / GMAIN.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  24KB  |  846 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. #define GEN
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include "hdr.h"
  15. #include "vars.h"
  16. #include "gvars.h"
  17. #include "libhdr.h"
  18. #include "segment.h"
  19. #include "ifile.h"
  20. #include "dbxp.h"
  21. #include "packp.h"
  22. #include "g0ap.h"
  23. #include "dclmapp.h"
  24. #include "arithp.h"
  25. #include "axqrp.h"
  26. #include "axqwp.h"
  27. #include "genp.h"
  28. #include "segmentp.h"
  29. #include "expandp.h"
  30. #include "procp.h"
  31. #include "libp.h"
  32. #include "libfp.h"
  33. #include "librp.h"
  34. #include "libwp.h"
  35. #include "readp.h"
  36. #include "setp.h"
  37. #include "initp.h"
  38. #include "glibp.h"
  39. #include "gutilp.h"
  40. #include "miscp.h"
  41. #include "gmiscp.h"
  42. #include "gmainp.h"
  43.  
  44. static void fold_upper(char *);
  45. static void preface();
  46. static void exitf(int);
  47. static void init_gen();
  48. static void finit_gen();
  49.  
  50. IFILE    *AISFILE, *AXQFILE, *STUBFILE, *LIBFILE, *TREFILE;
  51. int list_unit_0 = 0; /* set by '0' option to list unit 0 structure */
  52. int peep_option = 1; /* on for peep_hole optimization */
  53.  
  54. extern Segment    CODE_SEGMENT, DATA_SEGMENT, DATA_SEGMENT_MAIN;
  55. extern Tuple units_in_compilation;
  56. extern Segment   VARIANT_TABLE, FIELD_TABLE ;
  57.  
  58. #ifdef DEBUG
  59. extern int zpadr_opt; /* not for EXPORT */
  60. #endif
  61.  
  62. char *lib_name;
  63.  
  64. /*
  65. #include "avl.c"
  66. */
  67.  
  68. main (int argc, char **argv)
  69. {
  70.     Node     node_new ();
  71.     int        c, i, n;
  72.     int        errflg = 0, nobuffer = 0, mflag = 0;
  73.     extern int  optind;
  74.     extern char *optarg;
  75.     char    *fname, *tfname;
  76.     char    *t_name;
  77.  
  78. /*
  79.     AVL_GET_DESC();
  80. _outtext(" Generating Code.");
  81. */
  82.  
  83.     AISFILE = (IFILE *)0;
  84.     AXQFILE = (IFILE *)0;
  85.     LIBFILE = (IFILE *)0;
  86.     STUBFILE = (IFILE *)0;
  87.     TREFILE = (IFILE *)0;
  88.  
  89.     MAINunit = "";
  90.     interface_files = "";
  91.  
  92.  
  93.     while ((c = getopt (argc, argv, "g:l:m:ni:")) != EOF)
  94.         /*    user:
  95.          *    g    debugging, followed by list of options:
  96.          *        0    show structure of unit 0
  97.          *        b    do not buffer standard output
  98.          *        e    flag signalling errors in the parsing phase
  99.          *        g    list generated code
  100.          *        l    show line numbers in generated code
  101.          *        p    compiling predef units
  102.          *        z    call trapini to initialize traps
  103.          *      i   to specify object files and librairies for pragma interface
  104.          *      l    using library
  105.          *        m    main unit name
  106.          *      n    new library
  107.          */
  108.         switch (c) {
  109.         case 'i':
  110.             interface_files = strjoin(interface_files, optarg);
  111.             interface_files = strjoin(interface_files, " ");
  112.             break;
  113.         case 'l': /* using existing library */
  114.             lib_name= emalloc((unsigned) strlen(optarg) + 1);
  115.             strcpy(lib_name, optarg);
  116.             break;
  117.         case 'm': /* specify main unit name */
  118.             MAINunit = malloc((unsigned) strlen(optarg)+1);
  119.             strcpy(MAINunit, optarg);
  120.             fold_upper(MAINunit);
  121.             break;
  122.         case 'n': /* indicates new library */
  123.             new_library = TRUE;
  124.             break;
  125.         case 'g': /* gen debug options */
  126.             n = strlen(optarg);
  127.             for (i = 0; i < n; i++) {
  128.                 switch((int)optarg[i]) {
  129. #ifdef DEBUG
  130.                 case 'a':
  131.                     zpadr_opt = 0; /* do not print addresses in zpadr */
  132.                     break;
  133. #endif
  134.                 case 'g':
  135.                     list_code++;
  136.                     break;
  137.                 case 'l':
  138.                     line_option++;
  139.                     break;
  140.                 case 'p': /* compiling predef units */
  141.                     printf("compiling predef\n");
  142.                     compiling_predef++ ;
  143.                     break;
  144. #ifdef DEBUG
  145.                 case 'b': /* do not buffer output */
  146.                     nobuffer++;
  147.                     break;
  148.                 case 'd': /* force debugging output */
  149.                     debug_flag++;
  150.                     break;
  151.                 case 'e':
  152.                     errors = TRUE;
  153.                     break;
  154.                 case 'o': /* disable optimization (peep) */
  155.                     peep_option = 0;
  156.                     break;
  157.                 case '0': /* read trace including unit 0 */
  158.                     list_unit_0++;
  159.                     break;
  160.                 case 'z': 
  161.                     trapini();
  162.                     break;
  163. #endif
  164.                 }
  165.             }
  166.             break;
  167.         case '?':
  168.             errflg++;
  169.         }
  170.     fname = (char *)0;
  171.     if (optind < argc)
  172.         fname = argv[optind];
  173.     if (fname == (char *)0) errflg++;
  174.     if (errflg) {
  175.         fprintf (stderr, "Usage: adagen -aAbglnmMnrstw file\n");
  176.         exitp(RC_ABORT);
  177.     }
  178.     tup_init(); /* initialize set and tuple procedures */
  179.     FILENAME =  (fname != (char *)0) ? strjoin(fname, "") : fname;
  180.  
  181.     if (compiling_predef) {
  182.         PREDEFNAME = "";
  183.     }
  184.     else
  185.         PREDEFNAME = predef_env();
  186.     if (nobuffer) {
  187.         setbuf (stdout, (char *) 0);    /* do not buffer output (for debug) */
  188.     }
  189.     rat_init(); /* initialize arithmetic and rational package*/
  190.     dstrings_init(2048, 256); /* initialize dstrings package */
  191.     init_sem();
  192.     DATA_SEGMENT_MAIN = main_data_segment();
  193.     aisunits_read = tup_new(0);
  194.     init_symbols = tup_exp(init_symbols, seq_symbol_n);
  195.     for (i = 1; i <= seq_symbol_n; i++)
  196.         init_symbols[i] = seq_symbol[i];
  197.     t_name = libset(lib_name);
  198.  
  199.     num_predef_units = (compiling_predef) ? 0 : init_predef();
  200.  
  201.     /*
  202.      * When the separate compilation facility is being used all references to
  203.      * AIS files will be made via the directory in LIBFILE. AISFILENAME is set
  204.      * to a number.
  205.      */
  206.     if (compiling_predef)
  207.         AISFILENAME = "0";
  208.     else if (new_library)
  209.         AISFILENAME = "1";
  210.     else
  211.         AISFILENAME = lib_aisname(); /* retrieve name from library */
  212.  
  213.     /* open the appropriate files using the suffix .axq for axq files and
  214.      * .trc for tree file. 
  215.      *
  216.      * Open MESSAGEFILE with suffixe ".msg" if a file name specified;
  217.      * otherwise, if a file name not required, and one is not given,
  218.      * used stderr.
  219.      */
  220.     AXQFILE  = ifopen(AISFILENAME, "axq", "w", 0);
  221.  
  222.     MSGFILE = (FILENAME != (char *) 0 ) ? efopenl(FILENAME, "msg", "a", "t") :
  223.       stderr;
  224.  
  225.     /* delete any existing st2 file for this AISFILENAME since it is now
  226.      * obsolete
  227.      */
  228.     ifdelete(strjoin(AISFILENAME, ".st2"));
  229.     /* unbuffer output for debugging purposes */
  230.     if (MSGFILE != stderr)
  231.         setbuf(MSGFILE, (char *) 0);
  232.     preface();
  233.  
  234.     /* Code formerly procedure finit() in init.c is now put here directly */
  235.     if (!errors) {
  236.         write_glib();
  237.         cleanup_files();
  238.     }
  239.  
  240.     if (compiling_predef) printf("Compilation of predef complete\n");
  241.     exitf(RC_SUCCESS);
  242. }
  243.  
  244. static void fold_upper(char *s)                                /*;fold_upper*/
  245. {
  246.     register char c;
  247.  
  248.     while (c = *s) {
  249.         if (islower(c)) *s = toupper(c);
  250.         s++;
  251.     }
  252. }
  253.  
  254. void fold_lower(char *s)                    /*;fold_lower*/
  255. {
  256.     register char c;
  257.  
  258.     while (c = *s) {
  259.         if (isupper(c)) *s = tolower(c);
  260.         s++;
  261.     }
  262. }
  263.  
  264. /* In the SETL version, preface has the global declarations of macros and
  265.  * variables. In the C version, the global variables are defined in gvars.ch
  266.  * (from which gvars.c and gvars.h are derived); macros and structure
  267.  * declarations are in ghdr.h.
  268.  * This file is retained for now to hold parts of code not moved to other
  269.  * files in the C version.
  270.  *
  271.  * pref2 - part 2 of preface: global variables, procedure declarations 
  272.  *
  273.  * Conventions for capitalization.
  274.  * The SETL version uses upper case names for some procedures, macros
  275.  * and global variables. Since case conventions are not enforced by the
  276.  * SETL compiler, there are cases where the same name is written more 
  277.  * than one way, differing only in case.
  278.  
  279.  * In C, we will use upper case for macro names, defined constants and most
  280.  * of the global variables, especially, the variables defined here. Where
  281.  * mixed-case usage is known to exist in the SETL version, such will be
  282.  * indicated by writine (mixed-case) after the variable name.
  283.  */
  284.  
  285. /* macros moved to hdr.c*/
  286.  
  287. static Set units_loaded;
  288.  
  289. static void preface()                                        /*;preface*/
  290. {
  291.     int    indx, last_index, i, rootseq, body_number;
  292.     Node    first_node, unit_node;
  293.     Tuple    aisread_tup, tup;
  294.     int unit_number_now;
  295.     struct unit *pUnit;
  296.     char    *spec_nam;
  297.     aisread_tup = tup_new(0);
  298.     initialize_1();
  299.     /* 1- Load PREDEF */
  300.  
  301.     TASKS_DECLARED = FALSE;
  302.     /* 2- Generate user program */
  303.  
  304.     initialize_2();
  305.  
  306.     if (gen_option) {
  307.         /* read all the units in file, aisunits_read is tuple of unit names of
  308.          * units found in file.
  309.          */
  310.         TREFILE  = ifopen(AISFILENAME, "aic", "r", 0);
  311.         last_index = last_comp_index(TREFILE);
  312.         indx = 0;
  313.         units_loaded = set_new(0);
  314.         for (indx = 1; indx <= last_index; indx++) {
  315.             unit_name = read_ais(AISFILENAME, TRUE, (char *) 0, indx, TRUE);
  316.             TREFILE  = ifopen(AISFILENAME, "trc", "r", 0);
  317.             load_tre(TREFILE, indx);
  318.             unit_number_now = unit_numbered(unit_name);
  319.             pUnit = pUnits[unit_number_now];
  320.             seq_node_n = pUnit->treInfo.nodeCount;
  321.             seq_node = tup_new(seq_node_n);
  322.  
  323.             /* set seq_symbol to corresponding values